﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebDauGia.Domain.Concrete;
using WebDauGia._Library;
//using WebDauGia.Domain.Entities;
using WebDauGia.Domain.Models;

namespace WebDauGia.Controllers
{
    public class NhanVienController : Controller
    {
        //
        // GET: /NhanVien/
        //public EFDbContext db = new EFDbContext();
        public WebDauGiaDBContext db = new WebDauGiaDBContext();
        public NhanVien _user = new NhanVien();
        #region Xu ly Dang Nhap
        public Boolean DangNhap(String username, String raw_password)
        {
            if (username == null || raw_password == null)
                //băm mật khẩu nhập vào
                return false;
            String h_password = TextLibrary.GetSHA1HashData(raw_password);
            //lấy password từ db
            var db_pass = (from u in db.NhanViens
                           where u.username == username
                           && u.active == true
                           select u.password).FirstOrDefault();
            //kiểm tra lỗi 
            if (db_pass == null)
                return false;
            //xác nhận mật khẩu và return
            return h_password.Equals(db_pass.ToString()) ? true : false;
        }
#endregion
        #region Get Nhan vien by username and id
        public NhanVien get_by_username(String tendangnhap)
        {
            //if (tendangnhap == null) 
              //  return null;
            //else
              //  return db.NhanViens.FirstOrDefault(x=>x.username==tendangnhap);
            return db.NhanViens.First(x => x.username == tendangnhap);
        }
        public NhanVien get_by_id(int obj_id)
        {
            return db.NhanViens.FirstOrDefault(x => x.id == obj_id);
        }
        #endregion
        #region Change password
        public Boolean change_password(int obj_id, String old_raw_pass, String new_raw_pass)
        {
            //check
            if (!this.is_exist(obj_id) || old_raw_pass == null || new_raw_pass == null) return false;
            //get obj
            NhanVien obj = this.get_by_id(obj_id);
            //check old pass
            if (TextLibrary.GetSHA1HashData(old_raw_pass) == obj.password)
            {
                //hash password
                obj.password = TextLibrary.GetSHA1HashData(new_raw_pass);
                //commit
                this.db.SaveChanges();
                return true;
            }
            return false;
        }
        public Boolean set_password(int obj_id, String new_raw_or_hash_pass)
        {
            //check not exist or null
            if (!this.is_exist(obj_id) || new_raw_or_hash_pass == null) return false;
            //get obj
            NhanVien obj = this.get_by_id(obj_id);
            if (obj.password.Equals(new_raw_or_hash_pass))
            {
                //neu nhu new_raw_pass da dc hash thi bo qua//!!!!!!!!!!!!!!!!!!!!be careful
                return true;
            }
            //hash password
            obj.password = TextLibrary.GetSHA1HashData(new_raw_or_hash_pass);
            //commit
            this.db.SaveChanges();
            return true;
        }
        #endregion
        #region Get NhanVien by id and hash password
        public NhanVien get_by_id_hash_password(int obj_id, String hash_password)
        {
            if (hash_password == null) return null;
            NhanVien re = (from user in db.NhanViens
                           where user.id == obj_id
                           && user.password == hash_password
                           && user.active == true
                           select user).FirstOrDefault();
            return re;
        }
        #endregion
        #region Check exist id Nhan Vien 
        public Boolean is_exist(int id)
        {
            NhanVien u = (from user in db.NhanViens
                          where user.id == id
                          select user).FirstOrDefault();
            return u == null ? false : true;
        }
        #endregion
        public Boolean can_use_email(int obj_id, String email)
        {
            NhanVien u = (from user in db.NhanViens
                          where user.email.ToUpper().Equals(email.ToUpper())
                          && user.id != obj_id
                          select user).FirstOrDefault();
            return u == null ? true : false;
        }
        public Boolean can_use_tendangnhap(int obj_id, String username)
        {
            NhanVien u = (from user in db.NhanViens
                          where user.username.ToUpper().Equals(username.ToUpper())
                          && user.id != obj_id
                          select user).FirstOrDefault();
            return u == null ? true : false;
        }
        #region add and delete
        public int add(NhanVien obj)
        {
            //hash password first
            obj.password = TextLibrary.GetSHA1HashData(obj.password);
            //call add
            this.db.NhanViens.Add(obj);
            //commit
            this.db.SaveChanges();
            //return ma moi nhat
            return this.db.NhanViens.Max(x => x.id);
        }

        public Boolean delete(int id)
        {
            NhanVien obj = this.db.NhanViens.Where(x => x.id == id).FirstOrDefault();
            if (obj == null) return false;
            this.db.NhanViens.Remove(obj);
            this.db.SaveChanges();
            return true;
        }
        #endregion
        #region[tim kiem nhan vien]
        /*
        public List<NhanVien> timkiem(String id = "", String tendangnhap = "", String tendaydu = "", String email = "", String active = "", String loainhanvien_id = "", String forgot_password_session = "")
        {
            List<NhanVien> obj_list = new List<NhanVien>();
            //find by LIKE element
            obj_list = this._db.ds_nhanvien.Where(x => x.tendangnhap.Contains(tendangnhap)
                && x.tendaydu.Contains(tendaydu)
                && x.email.Contains(email)).ToList();
            //filter by id
            if (!id.Equals(""))
            {
                int id_i = TextLibrary.ToInt(id);
                obj_list = obj_list.Where(x => x.id == id_i).ToList();
            }

            //filter by session
            if (!forgot_password_session.Equals(""))
            {
                obj_list = obj_list.Where(x => x.forgot_password_session.ToUpper().Equals(forgot_password_session.ToUpper())).ToList();
            }

            //Filter again by by active
            if (!active.Equals(""))
            {
                Boolean active_b = active.Equals("1") ? true : false;
                obj_list = obj_list.Where(x => x.active == active_b).ToList();
            }

            //Filter again by by group_name
            if (!loainhanvien_id.Equals(""))
            {
                int lnv_id = TextLibrary.ToInt(loainhanvien_id);
                obj_list = obj_list.Where(x => x.loainhanvien.id == lnv_id).ToList();
            }

            //FINAL return
            return obj_list;
        }*/
        #endregion

        #region Validate NhanVien
        public List<String> validate(NhanVien obj, 
            String matkhau = "", 
            String matkhau2 = "")
        {
            //
            List<String> re = new List<string>();
            //check
            if (!this.can_use_tendangnhap(obj.id, obj.username))
            {
                re.Add("tendangnhap_exist_fail");
            }
            if (!this.can_use_email(obj.id, obj.email))
            {
                re.Add("email_exist_fail");
            }
            if (obj.email.Equals("") || !ValidateLibrary.is_valid_email(obj.email))
            {
                re.Add("email_fail");
            }
            if (obj.username.Equals(""))
            {
                re.Add("tendangnhap_fail");
            }
            if (obj.hoten.Equals(""))
            {
                re.Add("tendaydu_fail");
            }
            if (!matkhau.Equals(matkhau2))
            {
                re.Add("matkhau_fail");
            }
            return re;
        }

        #endregion
        
    }
}
